package da.metricscraft.client.frames;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import da.metricscraft.client.MetricsCraftClient;

public class SwitchPanel extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static SwitchPanel currentPanel;
	private JComboBox<String> playerComboBox;
	private JButton graphicsButton;
	private JButton statsButton;
	public enum ButtonActions{
		GRAPHICS,
		STATS
	}
	public static SwitchPanel getInstance(){
		if (currentPanel == null){
			currentPanel = new SwitchPanel();
		}
		return currentPanel;
	}
	public SwitchPanel(){
		setPreferredSize(new Dimension(870,700));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JPanel tabsection = new JPanel();
		tabsection.setPreferredSize(new Dimension(870,40));
		tabsection.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		initialize(tabsection);
		
		add(tabsection);
		add(GraphicsPanel.getInstance());
		GraphicsPanel.getInstance().validate();
	}
	public void enableButton(ButtonActions b){
		JPanel addingPanel;
		JPanel removingPanel;
		boolean enableStats = (b == ButtonActions.GRAPHICS)?true:false;
		
		if (enableStats){
			addingPanel = GraphicsPanel.getInstance();
			removingPanel = StatsPanel.getInstance();
		}else{
			addingPanel = StatsPanel.getInstance();
			removingPanel = GraphicsPanel.getInstance();
		}
		remove(removingPanel);
		add(addingPanel);
		addingPanel.revalidate();
		addingPanel.repaint();
		if (GraphicsPanel.getInstance().isEnabled())
			graphicsButton.setEnabled(!enableStats);
		if (StatsPanel.getInstance().isEnabled())
			statsButton.setEnabled(enableStats);
		
	}
	public void updateComboBox(ArrayList<String> players){
		
	}
	@Override
	public void repaint(){
		super.repaint();
		if (getParent() != null)
			getParent().repaint();
	}
	private void initialize(JPanel tabsection){
		graphicsButton = new JButton("Graphics");
		statsButton = new JButton("Stats");
		graphicsButton.setEnabled(false);
		statsButton.setEnabled(false);
		graphicsButton.setActionCommand(ButtonActions.GRAPHICS.name());
		statsButton.setActionCommand(ButtonActions.STATS.name());
		graphicsButton.addActionListener(this);
		statsButton.addActionListener(this);
		tabsection.add(graphicsButton);
		tabsection.add(statsButton);
		
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		enableButton(ButtonActions.valueOf(event.getActionCommand()));
	}

}
